import axios from 'axios';

class CozeWorkflowRunner {
  constructor() {
    this.apiBaseUrl = 'https://api.coze.cn/v1';
    this.headers = {
      Authorization: `Bearer pat_Ul8RJhYaqBBWH0z6jFUESrz4gKG7kUadL4YY3OI19n6tfnwVXZ70owUfJj6wXPS7`,
      'Content-Type': 'application/json'
    };

    this.axiosInstance = axios.create({
      baseURL: this.apiBaseUrl,
      headers: this.headers
    });
  }

  async runWorkflow(workflowId, parameters = {}) {
    const body = {
      workflow_id: workflowId,
      parameters
    };

    try {
      const response = await this.axiosInstance.post('/workflow/run', body);
      return response.data;
    } catch (error) {
      if (error.response) {
        // The request was made and the server responded with a status code
        // that falls out of the range of 2xx
        console.error('Error response:', error.response.data);
      } else if (error.request) {
        // The request was made but no response was received
        console.error('No response received:', error.request);
      } else {
        // Something happened in setting up the request that triggered an Error
        console.error('Error setting up request:', error.message);
      }
      throw error;
    }
  }
}

export default CozeWorkflowRunner;